<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	
	$loginuser = getUserInfo();
	
	if(empty($loginuser))
	{
		$result['sign'] = '-2';
		echoJSON($result);
		exit;
	}
	
	
	$m =  getParam('m','post');
	
	$result['sign'] = '0';
	
	$parm = requestArrayParm($_REQUEST);
	
	
	if($m == 'saveinfo')
	{
		$where['id'] = $loginuser['id'];
		$data['nickname'] = $parm['nickname'];
		$data['wx'] = $parm['wx'];
		$data['qq'] = $parm['qq'];
		$upres = _User::setUserInfo($where, $data);
		if($upres)
		{
			$result['msg'] = '保存成功';
			$result['sign'] = '1';
			echoJSON($result);
			exit;
		}
		else
		{
			$result['msg'] = '保存成功';
			echoJSON($result);
			exit;
		}
	}
	
	if($m == 'getChanpinItem')
	{
		if($parm['pid'])
		{
			unset($wheres,$sort,$pages);
			
			$resData = _Tongji_log::getUserbrowseListByItemID($loginuser['id'], $parm['pid']);
			
			$result['sign'] = '1';
			$result['items'] = $resData;
			
			echoJSON($result);
		}
	}
	
	if($m == 'saveApply_tixian')
	{
		//判断是否已经添加了账号信息
		unset($wheres);
		$wheres['uid'] = $loginuser['id'];
		$list = _Account_number::getAccount_numberList($wheres);
		
		if(empty($list))
		{
			$result['msg'] = '账号信息不完善';
			$result['sign'] = '99';
			echoJSON($result);
			exit;
		}
		
		if(!empty($parm['money']) && doubleval($parm['money']) > 0)
		{
			unset($wheres);
			$wheres['id'] = $loginuser['id'];
			$info = _User::getUserInfo($wheres);
			if($info['account'] >= round($parm['money'],2))
			{
				unset($wheres,$datas);
				$wheres['uid'] = $info['id'];
				$wheres['money'] = round($parm['money'],2);
				
				$datas['uid'] = $loginuser['id'];
				$datas['money'] = round($parm['money'],2);
				$datas['status'] = '1';
				$datas['addtime'] = time();
				$datas['phone'] = $info['loginname'];
				$chulires = _Tixian::chuliUserTixian($wheres,$datas);
				if($chulires)
				{
					$result['msg'] = '申请成功';
					$result['sign'] = '1';
					echoJSON($result);
					exit;
				}
				else
				{
					$result['msg'] = '申请失败,请重试';
					echoJSON($result);
					exit;
				}
			}
			else
			{
				$result['msg'] = '账户余额不足';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['msg'] = '数据不全';
			echoJSON($result);
			exit;
		}
	}
	
	
	if($m == 'saveApply_fanli')
	{
		if(!empty($parm['chanpin']) && !empty($parm['item']) && !empty($parm['money']))
		{
			unset($wheres);
			$wheres['pid'] = $parm['chanpin'];
			$wheres['id'] = $parm['item'];
			
			$item = _ChanPin::getChanPin_ItemInfo($wheres);
			
			if($item)
			{
				$data['pid'] = $item['pid'];
				$data['iid'] = $item['id'];
				$data['money'] = floatval($parm['money']);
				$data['uid'] = $loginuser['id'];
				$data['phone'] = $loginuser['loginname'];
				$data['addtime'] = time();
				$data['status'] = '1';
				
				if($item['type'] == '1')
				{
					$data['fanli'] = $data['money'] * ($item['fanli'] / 100);
				}
				
				if($item['type'] == '2')
				{
					$data['fanli'] = $item['fanli'];
				}
				
				$addres = _Fanli::addFanliInfo($data);
				if($addres)
				{
					$result['sign'] = '1';
					$result['msg'] = '提交成功，您的返利金额为：'.$data['fanli'].'元。';
				}
				else
				{
					$result['msg'] = '保存出错,申请失败';
				}
				echoJSON($result);
				exit;
			}
			else
			{
				$result['msg'] = '项目不存在';
				echoJSON($result);
				exit;
			}
		}
		else
		{
			$result['msg'] = '数据不全';
			echoJSON($result);
			exit;
		}
	}
	
	if($m == 'save_number')
	{

		if(!empty($parm['info']))
		{
			foreach ($parm['info'] as $key => $value)
			{
				unset($info);
				if($value['id'])
				{
					unset($wheres);
					$wheres['id'] = $value['id'];
					$wheres['uid'] = $loginuser['id'];
					$info = _Account_number::getAccount_numberInfo($wheres);
				}
				//如果已经存在则更新,不存在添加

				if($info)
				{
					
					if(!empty($value['number']) && !empty($value['name']) && !empty($value['institution']))
					{
						unset($where, $data);
						$where['id'] = $info['id'];
						$data['number'] = $value['number'];
						$data['name'] = $value['name'];
						$data['institution'] = $value['institution'];
						_Account_number::setAccount_numberInfo($where, $data);
					}
					else
					{

						unset($where, $data);
						$where['id'] = $info['id'];
						_Account_number::delAccount_numberInfo($where);
					}
				}
				else
				{
					if(!empty($value['number']) && !empty($value['name']) && !empty($value['institution']))
					{
						
						unset($data);
						$data['uid'] = $loginuser['id'];
						$data['type'] = $value['type'];
						$data['name'] = $value['name'];
						$data['number'] = $value['number'];
						$data['institution'] = $value['institution'];
						$data['addtime'] = time();
						_Account_number::addAccount_numberInfo($data);
					}
					
				}
				
			}
			
			$result['msg'] = '保存成功';
			$result['sign'] = '1';
			echoJSON($result);
		}
		else
		{
			$result['msg'] = '请输入完整渠道信息';
			echoJSON($result);
			exit;
		}
		
		
	}